package com.apple1.blogservices.impl;

import com.apple1.blogdao.ArticleMapper;
import com.apple1.blogdao.CommunityManagerMapper;
import com.apple1.blogdao.CommunityMapper;
import com.apple1.blogentity.*;
import com.apple1.blogentity.vo.ArticleVo;
import com.apple1.blogentity.vo.CommunityVo;
import com.apple1.blogservices.CommunityService;
import com.apple1.commonutil.BeanMapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author chencheng
 */
@Service
public class CommunityServiceImpl implements CommunityService {

    @Autowired
    CommunityMapper communityMapper;

    @Autowired
    CommunityManagerMapper communityManagerMapper;

    @Autowired
    ArticleMapper articleMapper;
    /**
     * 方法名: 添加圈子
     * 处理逻辑: 1.插入圈子信息 2.插入圈子的管理员
     * note(注意): 事务
     * @param communityVo
     * @return int 0-失败 1-成功
     */
    @Override
    public int addCommunity(CommunityVo communityVo) {
        if (communityVo == null || communityVo.getCommunityName() == null ||communityVo.getCreatorId() == null){
            return 0;
        }
        Community mapBean = BeanMapUtil.getMapBean(communityVo, Community.class);
        mapBean.setTableId(null);

        int id = communityMapper.insertSelective(mapBean);

        CommunityManager communityManager = new CommunityManager();
        communityManager.setCommunityId(mapBean.getTableId());
        communityManager.setUserId(mapBean.getCreatorId());
        communityManager.setFlag(0);//0是管理员 非0为其他用户

        return communityManagerMapper.insertSelective(communityManager);
    }
    /**
     * 方法名: 添加圈子的成员
     * 处理逻辑: 1.检查用户id是否是圈子管理员 2.如果不是圈子管理员，添加用户（如果存在就是更新下数据）
     * note(注意): 0-管理员 1-普通用户
     * @param user_id
     * @param community_id
     * @return int 0-失败 1-成功
     */
    @Override
    public int addCommunityUser(Integer user_id, Integer community_id) {
        if (user_id == null || community_id == null) {
            return 0;
        }
        if (isUserCommunityManager(user_id,community_id)){
            return 0;
        }

        CommunityManager communityManager = new CommunityManager();
        communityManager.setCommunityId(community_id);
        communityManager.setUserId(user_id);
        communityManager.setFlag(1);//0是管理员 非0为其他用户

        return communityManagerMapper.insertSelective(communityManager);
    }


    /**
     * 方法名：是否是圈子管理员
     * @param user_id
     * @param community_id
     * @return true->是圈子管理员；
     */
    @Override
    public boolean isUserCommunityManager(Integer user_id, Integer community_id) {
        if (user_id == null || community_id == null) {
           throw new RuntimeException("无法判断用户是否是这个圈子的管理员");
        }
        CommunityManagerExample communityManagerExample = new CommunityManagerExample();
        CommunityManagerExample.Criteria criteria = communityManagerExample.createCriteria();
        criteria.andUserIdEqualTo(user_id).andCommunityIdEqualTo(community_id).andFlagEqualTo(0);

        long num = communityManagerMapper.countByExample(communityManagerExample);
        return num != 0 ;
    }


    /**
     * 方法名: 添加圈子的管理员
     * 处理逻辑: 1.首先判断用户是否在圈子内 2.添加管理员
     * note(注意):
     * @param user_id
     * @param community_id
     * @return int 0-失败 非0-成功
     */
    @Override
    public int addCommunityManager(Integer user_id, Integer community_id) {
        if (user_id == null || community_id == null) {
            return 0;
        }
        CommunityManager communityManager = new CommunityManager();
        communityManager.setCommunityId(community_id);
        communityManager.setUserId(user_id);
        communityManager.setFlag(0);   //0是管理员 非0为其他用户
        if (isUserInCommunity(user_id,community_id)){
            CommunityManagerExample communityManagerExample = new CommunityManagerExample();
            CommunityManagerExample.Criteria criteria = communityManagerExample.createCriteria();
            criteria.andUserIdEqualTo(user_id);
            criteria.andCommunityIdEqualTo(community_id);

          return communityManagerMapper.updateByExampleSelective(communityManager,communityManagerExample);
        }else{
            return communityManagerMapper.insertSelective(communityManager);
        }

    }

    /**
     * 方法名: 根据圈子id删除圈子
     * 处理逻辑: 1.删除圈子 2.删除圈子的成员
     * note(注意): 事务
     * @param community_id
     * @return int 0-失败 非0-成功
     */
    @Override
    public int delCommunity(Integer community_id) {
        if (community_id == null){
            return 0;
        }

        CommunityExample communityExample = new CommunityExample();
        CommunityExample.Criteria criteria = communityExample.createCriteria();
        criteria.andTableIdEqualTo(community_id);

        communityMapper.deleteByExample(communityExample);


        CommunityManagerExample communityManagerExample = new CommunityManagerExample();
        CommunityManagerExample.Criteria criteria1 = communityManagerExample.createCriteria();
        criteria1.andCommunityIdEqualTo(community_id);

        return  communityManagerMapper.deleteByExample(communityManagerExample);

    }

    /**
     * 方法名: 删除圈子的某个成员
     * 处理逻辑:
     * note(注意):
     * @param user_id
     * @param community_id
     * @return int
     */
    @Override
    public int delCommunityUser(Integer user_id, Integer community_id) {
        if (user_id == null || community_id == null){
            return 0;
        }
        CommunityManagerExample communityManagerExample = new CommunityManagerExample();
        CommunityManagerExample.Criteria criteria1 = communityManagerExample.createCriteria();
        criteria1.andCommunityIdEqualTo(community_id).andUserIdEqualTo(user_id);

        return  communityManagerMapper.deleteByExample(communityManagerExample);
    }

    /**
     * 方法名: 根据用户id取消当前所在圈子的某个用户的管理员权限
     * 处理逻辑:
     * note(注意):
     * @param user_id
     * @param community_id
     * @return int 0-失败 非0-成功
     */
    @Override
    public int changeCommunityManager(Integer user_id, Integer community_id) {
        if (isUserInCommunity(user_id, community_id)){
            CommunityManager communityManager = new CommunityManager();
            communityManager.setCommunityId(community_id);
            communityManager.setUserId(user_id);
            communityManager.setFlag(1);//0是管理员 非0为其他用户

            CommunityManagerExample communityManagerExample = new CommunityManagerExample();
            CommunityManagerExample.Criteria criteria1 = communityManagerExample.createCriteria();
            criteria1.andCommunityIdEqualTo(community_id).andUserIdEqualTo(user_id);

            return communityManagerMapper.updateByExampleSelective(communityManager,communityManagerExample);
        }

        return 0;
    }
    /**
     * 方法名: 更新圈子的信息
     * 处理逻辑:
     * note(注意):
     * @param community_id
     * @param communityVo
     * @return int 0-失败 非0-成功
     */
    @Override
    public int updateCommunity(Integer community_id, CommunityVo communityVo) {
        if  (community_id == null || communityVo == null || communityVo.getCreatorId() == null ||  communityVo.getCommunityName() == null){
            return 0;
        }

        Community mapBean = BeanMapUtil.getMapBean(communityVo, Community.class);
        CommunityExample communityExample = new CommunityExample();
        CommunityExample.Criteria criteria = communityExample.createCriteria();
        criteria.andTableIdEqualTo(mapBean.getTableId());
        mapBean.setTableId(null);

        return communityMapper.updateByExampleSelective(mapBean,communityExample);

    }
    /**
     * 方法名:根据圈子id查询圈子
     * 处理逻辑:
     * note(注意):
     * @param community_id
     * @return com.apple1.blogentity.vo.CommunityVo
     */
    @Override
    public CommunityVo selectCommunity(Integer community_id) {
        CommunityExample communityExample = new CommunityExample();
        CommunityExample.Criteria criteria = communityExample.createCriteria();
        criteria.andTableIdEqualTo(community_id);

        List<Community> communities = communityMapper.selectByExample(communityExample);

        if (communities == null || communities.size() == 0){
            return null;
        }
        Community community = communities.get(0);

        return BeanMapUtil.getMapBean(community,CommunityVo.class);
    }


    /**
     * 方法名: 查询圈子内所有用户的文章信息
     * 处理逻辑:
     * note(注意): 这里没有加 文章的标签列表
     * @param community_id
     * @return java.util.List<com.apple1.blogentity.vo.ArticleVo>
     */
    @Override
    public List<ArticleVo> selectCommuintyUserArticles(Integer community_id) {
        if (community_id == null){
            return null;
        }
        CommunityManagerExample communityManagerExample = new CommunityManagerExample();
        CommunityManagerExample.Criteria criteria = communityManagerExample.createCriteria();
        criteria.andCommunityIdEqualTo(community_id);


        List<CommunityManager> communityManagers = communityManagerMapper.selectByExample(communityManagerExample);

        if (communityManagers == null || communityManagers.size()==0){
            return null;
        }
        List<ArticleVo> list = new ArrayList<>();
        for (CommunityManager communityManager:communityManagers){

            Integer userId = communityManager.getUserId();
            ArticleExample articleExample = new ArticleExample();
            ArticleExample.Criteria criteria1 = articleExample.createCriteria();
            criteria1.andUserIdEqualTo(userId);

            List<Article> articles = articleMapper.selectByExample(articleExample);
            if (articles == null || articles.size() == 0){
                continue;
            }else {
                for (Article article:articles){
                    ArticleVo mapBean = BeanMapUtil.getMapBean(article, ArticleVo.class);
                    list.add(mapBean);
                }
            }

        }

        return list;
    }


    /**
     *  用户是否在圈子内
     * @param user_id
     * @param community_id
     * @return true 存在圈子内
     */
    @Override
    public boolean isUserInCommunity(Integer user_id, Integer community_id) {
        if (user_id == null || community_id == null) {
            throw new RuntimeException("无法判断用户是否在这个圈子里面");
        }
        CommunityManagerExample communityManagerExample = new CommunityManagerExample();
        CommunityManagerExample.Criteria criteria = communityManagerExample.createCriteria();
        criteria.andUserIdEqualTo(user_id).andCommunityIdEqualTo(community_id);

        long num = communityManagerMapper.countByExample(communityManagerExample);
        return num != 0 ;
    }
}
